﻿using Colorful.Models;
using Microsoft.AspNetCore.Authentication;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Logging;
using Microsoft.Net.Http.Headers;
using Colorful.Database;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;

namespace Colorful.CMS.Core
{
    public abstract class BaseController : Microsoft.AspNetCore.Mvc.Controller
    {
        #region 私有变量
        private ILogger _logger;
        private MySession _session = null;
        private string _lang = null;
        private Client _client;
        private AppUser _appUser;
        private string _userId;
        private string _loginId;
        private string _url;
        private UserPermission _userPermission;
        private List<MenuJson> _userMenus;
        #endregion

        #region 属性

        #region 语言
        /// <summary>
        /// 当前语言
        /// </summary>
        public string Lang
        {
            get
            {
                if (_lang == null)
                {
                    _lang = $"{this.HttpContext.Items["Lang"]}";
                    if (string.IsNullOrEmpty(_lang))
                        _lang = "CN";
                }
                return _lang;
            }
        }
        /// <summary>
        /// 网站语言列表
        /// </summary>
        protected List<JsonData<string>> Languages
        {
            get
            {
                return WebService.Languages;
            }
        }
        #endregion

        #region Session
        /// <summary>
        /// Session对象
        /// </summary>
        protected MySession Session
        {
            get
            {
                if (_session == null)
                    _session = new MySession(HttpContext.Session);
                return _session;
            }
        }
        #endregion

        #region 客户端信息
        /// <summary>
        /// 客户端信息
        /// </summary>
        public Client Client
        {
            get
            {
                if (_client == null)
                {
                    _client = this.HttpContext.GetClient();
                }
                return _client;
            }
        }
        #endregion

        #region 用户信息
        /// <summary>
        /// 当前登陆用户唯一Id
        /// </summary>
        protected string UserId
        {
            get
            {
                if (_userId == null)
                {
                    _userId = this.User.GetId();
                }
                return _userId;
            }
        }
        /// <summary>
        /// 当前用户的登陆Id
        /// </summary>
        protected string LoginId
        {
            get
            {
                if (_loginId == null)
                {
                    _loginId = this.User.GetLoginId();
                }
                return _loginId;
            }
        }
        /// <summary>
        /// 当前登录用户对象
        /// </summary>
        protected AppUser AppUser
        {
            get
            {
                if (_appUser == null)
                {
                    _appUser = this.HttpContext.GetAppUser();
                }
                return _appUser;
            }
        }
        #endregion

        #region DomainUrl 获取当前域名地址
        /// <summary>
        /// 获取当前域名地址
        /// </summary>
        protected string DomainUrl
        {
            get
            {
                if (_url == null)
                {
                    _url = Request.GetDomainUrl();
                }
                return _url;
            }
        }
        #endregion

        #region 网站设置
        protected WebSetting WebSetting
        {
            get
            {
                return WebService.WebSetting;
            }
        }
        #endregion

        #endregion

        #region 内部方法

        #region 获取语文字符
        protected T GetLang<T>(T cn, T en)
        {
            return this.HttpContext.GetLang(cn, en);
        }
        #endregion

        #region 获取用户权限
        protected UserPermission GetUserPermission(AppUser user = null)
        {
            if (_userPermission == null)
                _userPermission = this.HttpContext.GetUserPermission(user);
            return _userPermission;
        }
        #endregion

        #region 获取用户菜单
        protected List<MenuJson> GetUserMenus(int menuType, Func<List<long>, Menu, bool> filter)
        {
            if (_userMenus == null)
                _userMenus = this.HttpContext.GetUserMenus(menuType, filter);
            return _userMenus;
        }
        #endregion

        #region 用户登录
        protected void Login(AppUser user)
        {
            this.HttpContext.Login(user);
        }
        #endregion

        #endregion
    }
}
